﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using CustomControl;
using TeacherLeftHand.Model;
using TeacherLeftHand.Util;

namespace TeacherLeftHand.View
{
    public class InlineRichTextBox:RichTextEditor
    {
        public static RoutedEvent InlineRtbLostFocusEvent = EventManager.RegisterRoutedEvent("InlineRtbLostFocus", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(RichTextEditor));
        //public static RoutedEvent InlineRtbTextChangedEvent = EventManager.RegisterRoutedEvent("InlineRtbTextChanged", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(RichTextEditor));
        public InlineRichTextBox():base()
        {
           
            this.AddHandler(RichTextBox.DropEvent, new DragEventHandler(rtbPreparation_Drop), true);
            this.AddHandler(RichTextBox.DragEnterEvent, new DragEventHandler(rtbPreparation_DragEnter), true);
            this.AddHandler(RichTextBox.DragOverEvent, new DragEventHandler(rtbPreparation_DragOver), true);
            this.LostFocus += new RoutedEventHandler(InlineRichTextBox_LostFocus);
            //this.TextChanged += new TextChangedEventHandler(InlineRichTextBox_TextChanged);
        }

        //void InlineRichTextBox_TextChanged(object sender, TextChangedEventArgs e)
        //{
        //    this.RaiseEvent(new RoutedEventArgs(InlineRtbTextChangedEvent));
        //}

        void InlineRichTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            this.RaiseEvent(new RoutedEventArgs(InlineRtbLostFocusEvent));
        }


        private void rtbPreparation_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(KnowledgeItem)))
            {
                e.Effects = DragDropEffects.Copy;
            }else if (e.Data.GetDataPresent(DataFormats.StringFormat))
            {
                e.Effects = DragDropEffects.None;
            }
        }

        private void rtbPreparation_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(KnowledgeItem)))
            {
                e.Effects = DragDropEffects.Copy;
            }
        }

        private void rtbPreparation_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(KnowledgeItem)))
            {
                var droppedValue = (KnowledgeItem)e.Data.GetData(typeof(KnowledgeItem));
                Utility.LoadRTF(droppedValue.Content, (RichTextEditor)sender);
            }
        }
      
    }
}
